package week3;
import java.util.Random;
import java.util.Scanner;


public class GuessNumber {
    public static void main(String[] args) {
        GuessNumber.guessnumber();
    }

    public static void guessnumber() {
        Random random = new Random();
        int targetNumber = random.nextInt(10) + 1; //0-9变为1-10,生成1到10的随机数
        Scanner scanner = new Scanner(System.in);
        int guess;
        int attempts = 3; //3次机会
        System.out.println("欢迎来到猜数游戏！请猜一个1到10之间的整数，你有三次机会。");
        while (attempts > 0) {
            System.out.println("请输入你的猜测：");
            guess = scanner.nextInt();
            if (guess == targetNumber) {
                System.out.println("恭喜你，猜对了！");
                break;
            } else if (guess < targetNumber) {
                attempts--;
                System.out.println("你猜的数小了，还剩" + attempts + "次机会。");
            } else {
                attempts--;
                System.out.println("你猜的数大了，还剩" + attempts + "次机会。");
            }

            if (attempts == 0) {
                System.out.println("很遗憾，你的机会用完啦。。正确答案是" + targetNumber + "。");
            }
        }
    }
}

